define(
    [
        "dojo/dom",
        "dojo/on",
        "dojo/_base/array",
        "dojo/_base/lang",
        "dojo/topic",
        "esri/layers/GraphicsLayer",
        "esri/layers/ArcGISTiledMapServiceLayer",
        "esri/map"
    ],
    function (dom, on, array, lang, topic,GraphicsLayer,ArcGISTiledMapServiceLayer,Map
    		 ) {
    	return {
    		maps:[],
    		eventTimer:null,
    		mapOne_handle:null,
    		mapTwo_handle:null,
    		mapThree_handle:null,
    		mapFour_handle:null,
    		highLightLayer:null,
    		clickOne:false,
    		clickTwo:false,
    		clickThree:false,
    		clickFour:false,
    		bManu:false,
    		mainMapClickHandler:null,
    		
    		options : {
                    extent:ioc.map.initExt,
                    slider:false,
                    lods:ioc.map.lods,
                    logo:false
                },
    		
            init:function(mainMap){
            	
            	Global.activeMapId = '';
            	//if(this.mainMapClickHandler)this.mainMapClickHandler.remove();
            	this.bManu = false;
            	this.clearExtentchange();
    			this.options.extent = mainMap.extent;
    			this.clickOne = false;
    			this.clickTwo = false;
    			this.clickThree = false;
    			this.clickFour = false;
    			this.removeFocus();
            	if(Global.maps.length==0){
    				Global.maps.push(mainMap);
    			}
            },
                
    		createMap : function(mapCount,mainMap){
    		     debugger;
    			if(mapCount==Global.maps.length)return;
    			this.init(mainMap);
    			
    			if(Global.maps.length<mapCount){
    				for(var i=Global.maps.length;i<mapCount;i++){
        				$('#mainArea').append('<div id="map' + (i+1) + '"  class="mapLayout" ></div>');
        			}
    			}
    			if(mapCount==1){
    				$("#map").css({'width':'100%','height':'100%'});
    				if(this.mainMapClickHandler)this.mainMapClickHandler.remove();
//    				if(dijit.byId(Global.maps[0].__container.id)){
//    					dijit.byId(Global.maps[0].__container.id).destroy();
//    				}
    				
    			}else if(mapCount==2){
    				$("#map").css({'width':'50%','height':'100%'});
    				$("#map2").css({'width':'50%','height':'100%','padding':'2px'});
    			}else if(mapCount==3){
    				$("#map").css({'width':'50%','height':'100%'});
    				$("#map2").css({'width':'50%','height':'50%','padding':'2px'});
    				$("#map3").css({'width':'50%','height':'50%','padding':'2px'});
    			}else if(mapCount==4){
    				$("#map").css({'width':'50%','height':'50%'});
    				$("#map2").css({'width':'50%','height':'50%','padding':'2px'});
    				$("#map3").css({'width':'50%','height':'50%','padding':'2px'});
    				$("#map4").css({'width':'50%','height':'50%','padding':'2px'});
    			}
    			
    			if(Global.maps.length<mapCount){
    				for(var i=Global.maps.length;i<mapCount;i++){
        				var map = new Map('map'+(i+1), this.options);
        				Global.maps.push(map);
                        var basemap = new ArcGISTiledMapServiceLayer(ioc.map.basemap,{id:"basemap"});   //部署时打开
                        map.addLayer(basemap,0);
                        basemap.isBasemapLayer = true;//标识为基础底图
                        this.highLightLayer=new GraphicsLayer({id:"highLightLayer"});
                    	map.addLayer(this.highLightLayer);
                        //地图获取焦点
                        map.on('click', lang.hitch(this,this._mapClickEvent));
        			}
    				this.mainMapClickHandler = mainMap.on('click', lang.hitch(this,this._mapClickEvent));
    			}else if(Global.maps.length>mapCount){
    				for(var i=Global.maps.length;i>mapCount;i--){
    					var labelswidget = dijit.byId(Global.maps[i-1].__container.id);
    					if(labelswidget){
    						labelswidget.destroy();
    					}
    					Global.maps[i-1].destroy();
    					$('#map'+i).remove();
    					Global.maps.splice(i-1,1);
    				}
    			}
    			for(var i=0;i<Global.maps.length;i++){
    				Global.maps[i].resize(true);
    				Global.maps[i].setExtent(new esri.geometry.Extent(this.options.extent));
    			}
    			if(mapCount!=1){
    				Global.activeMapId = 'map';
    				$('#map').addClass('activeMap');
    				this._addEvent();
    			}
    		},
    		 
    		_mapClickEvent:function(evt){
    			this.removeFocus();
    			Global.activeMapId=evt.currentTarget.id.split('_')[0];
    			$('#' + Global.activeMapId).addClass('activeMap');
    			if(Global.maps.length==0||Global.maps.length==1){
    				$('#map').removeClass('activeMap');
    			}
    		},
    		
    		removeFocus:function(){
    			$('#map').removeClass('activeMap');
    			$('#map2').removeClass('activeMap');
    			$('#map3').removeClass('activeMap');
    			$('#map4').removeClass('activeMap');
    		},
    		
	    	// 设置map的bounds
	        mapSerView: function (options) {
	        	 
        		for (obj in Global.maps) {
	                if (Global.maps[obj]&&obj!='remove') {
	                    if (Global.maps[obj] == options.map) {
	                        continue;
	                    }
	                    if (Math.abs(options.extent.xmin - Global.maps[obj].extent.xmin) > 0.0001 ||
	                    Math.abs(options.extent.ymin - Global.maps[obj].extent.ymin) > 0.0001) {
	                    	Global.maps[obj].centerAndZoom(options.extent.getCenter(), options.zoom);
	                    }
	                }
	            }
	            this.clearExtentchange();
	            clearTimeout(this.eventTimer);
	            //创建新resize Timer，让它延迟0.4秒触发
	            tempThis = this;
	            this.eventTimer = setTimeout(function () {
	            	tempThis._addEvent();
	            }, 400);
	        },
	       
	        /*添加事件*/
	        _addEvent: function () {
	        	
	        	if(!this.bManu){
	        		if (Global.maps[0]) {
	 	                this.mapOne_handle = Global.maps[0].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[0]));
	 	            }
	 	            if (Global.maps[1]) {
	 	            	this.mapTwo_handle = Global.maps[1].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[1]));
	 	            }
	 	            if (Global.maps[2]) {
	 	            	this.mapThree_handle = Global.maps[2].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[2]));
	 	            }
	 	            if (Global.maps[3]) {
	 	            	this.mapFour_handle = Global.maps[3].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[3]));
	 	            }
	        	}
	        	
	            if (Global.maps[0] && this.clickOne) {
	                this.mapOne_handle = Global.maps[0].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[0]));
	                this.clickOne = false;
	                this.mapOne_movehandle = Global.maps[0].on('mouse-move', lang.hitch(this,this._moveEvent,1));
	            }
	            if (Global.maps[1] && this.clickTwo) {
	            	this.mapTwo_handle = Global.maps[1].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[1]));
	            	this.clickTwo = false;
	            	this.mapTwo_movehandle = Global.maps[1].on('mouse-move', lang.hitch(this,this._moveEvent,2));
	            }
	            if (Global.maps[2] && this.clickThree) {
	            	this.mapThree_handle = Global.maps[2].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[2]));
	            	this.clickThree = false;
	            	this.mapThree_movehandle = Global.maps[2].on('mouse-move', lang.hitch(this,this._moveEvent,3));
	            }
	            if (Global.maps[3] && this.clickFour) {
	            	this.mapFour_handle = Global.maps[3].on('extent-change', lang.hitch(this,this._extentchangeEvent, Global.maps[3]));
	            	this.clickFour = false;
	            	this.mapFour_movehandle = Global.maps[3].on('mouse-move', lang.hitch(this,this._moveEvent,4));
	            }
	        },
	        _extentchangeEvent: function (map,evt) {
	            var extent = evt.extent;
	            var mapZoom = map.getZoom();
	            this.mapSerView({ extent: extent, zoom: mapZoom, map: map });
	        },
	        _moveEvent:function(mapsCount,evt){
	        	if(mapsCount==1)this.clickOne=true;
	        	if(mapsCount==2)this.clickTwo=true;
	        	if(mapsCount==3)this.clickThree=true;
	        	if(mapsCount==4)this.clickFour=true;
	        	this.bManu=true;
	        },
	        clearExtentchange:function(){
	        	if(this.mapOne_handle)this.mapOne_handle.remove();
	        	if(this.mapTwo_handle)this.mapTwo_handle.remove();
        		if(this.mapThree_handle)this.mapThree_handle.remove();
        		if(this.mapFour_handle)this.mapFour_handle.remove();
	        }
    	};
         
    });